<?php

header('Content-type:text');
define("TOKEN","weixin");
$wechatObj = new wechatCallbackapiTest();
if(isset($_GET['echostr'])){
$wechatObj->valid();
}else{
  $wechatObj->responseMsg();
}

class wechatCallbackapiTest{
 public function valid(){
 $echoStr = $_GET["echostr"];
 if($this->checkSignature()){
 echo $echoStr;
 exit;
}
}
private function checkSignature()
{
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];
	
    $token = TOKEN;
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode( $tmpArr );
    $tmpStr = sha1( $tmpStr );
    
    if( $tmpStr == $signature ){
        return true;
    }else{
        return false;
    }
}


		 //这段代码是实现消息的回复


       


    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
        libxml_disable_entity_loader(true);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

            //用户发送的消息类型判断
            switch ($RX_TYPE)
            {
                case "text":    //文本消息
                    $result = $this->receiveText($postObj);
                    break;
                case "image":   //图片消息
                    $result = $this->receiveImage($postObj);
                    break;

                case "voice":   //语音消息
                    $result = $this->receiveVoice($postObj);
                    break;
                case "video":   //视频消息
                    $result = $this->receiveVideo($postObj);
                    break;
                case "location"://位置消息
                    $result = $this->receiveLocation($postObj);
                    break;
                case "link":    //链接消息
                    $result = $this->receiveLink($postObj);
                    break;
                case "event": //歌曲消息
                    $result = $this->receivEvent($postObj);
                    break;
                default:
                    $result = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            echo $result;
        }else {
            echo "";
            exit;
        }
    }

    /*
     * 接收文本消息
     */
    private function receiveText($object)
    {
         $content = "您发的是消息包含以下信息：\n发信人OpenID:".$object->FromUsername."\n收信人微信号：".$object->ToUsername."\n发新时间：".$object->CreateTime.
 "\n消息类型：".$object->MsgType."\n消息内容：".$object->Content."\n消息id:".$object->MsgId;
       
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收图片消息
     */
    private function receiveImage($object)
    {
        $content = "您发送的消息类型不是文本，而是：".$object->MsgType;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收语音消息
     */
    private function receiveVoice($object)
    {
        
 //        //语音
 //                $content = "您发的是消息包含以下信息：\n发信人OpenID:".$object->FromUsername."\n收信人微信号：".$object->ToUsername."\n发送时间：".$object->CreateTime.
 // "\n消息类型：".$object->MsgType."\n消息内容：".$object->Content."\n消息id:".$object->MsgId;
     $content = "消息内容".$object->MediaId.$object->Format;
        
        $result = $this->transmitText($object, $content);
        return $result;
        
    }

    /*
     * 接收视频消息
     */
    private function receiveVideo($object)
    {
          $content = "您发送的消息类型不是文本，而是：".$object->MsgType;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收位置消息
     */
    private function receiveLocation($object)
    {
        $content = "你发送的是位置，纬度为：".$object->Location_X."；经度为：".$object->Location_Y."；缩放级别为：".$object->Scale."；位置为：".$object->Label;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 接收链接消息
     */
    private function receiveLink($object)
    {
        $content = "你发送的是链接，标题为：".$object->Title."；内容为：".$object->Description."；链接地址为：".$object->Url;
        $result = $this->transmitText($object, $content);
        return $result;
    }

    /*
     * 回复文本消息
     */
    private function transmitText($object, $content)
    {
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }
    
    private function replayVoice($object, $content){
    $textTpl = "
 <xml>
  <ToUserName><![CDATA[toUser]]></ToUserName>
  <FromUserName><![CDATA[fromUser]]></FromUserName>
  <CreateTime>1357290913</CreateTime>
  <MsgType><![CDATA[voice]]></MsgType>
  <MediaId><![CDATA[media_id]]></MediaId>
  <Format><![CDATA[Format]]></Format>
  <MsgId>1234567890123456</MsgId>
</xml>
";
  $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    
    }
    
    /*
    *回复歌曲
    */
     private function  receivEvent($postObj){
      $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[text]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";   
         if($postObj->MsgType=='event'){
           if($postObj->Event == 'CLICK'){
             if($postObj->EventKey == 'V1001_TODAY_MUSIC'){

               $contentStr = "茂职院http://www.mmvtc.cn";
              $result = sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
              echo $result;
            }
          }
        }
     }
}
?>